package com.lambkit.module.upms;

import com.lambkit.Lambkit;
import com.lambkit.core.cache.CacheManager;
import com.lambkit.plugin.auth.cache.UserCacheBase;

public class UpmsCache extends UserCacheBase  {

	@Override
	public boolean useJedis() {
		UpmsConfig config = Lambkit.config(UpmsConfig.class);
		String upmsType = config.getType();
		if("server".equals(upmsType) && config.getCacheType().equals(CacheManager.TYPE_REDIS)) {
			return true;
		}
		return isServerRedis() ? true : false;
	}

	/**
	 * // 判断是否已登录，如果已登录，则回跳，防止重复登录
	 * @param sessionId
	 * @return
	 */
	public String getSession(String sessionId) {
		return get(UpmsConstant.LAMBKIT_UPMS_SERVER_SESSION_ID + "_" + sessionId);
	}

	public String getCode(String sessionId, String userName) {
		//UUID.randomUUID().toString();
		return sessionId + "&" + userName;
	}

	public void saveSession(String sessionId, String userName, int timeOut) {
		// 全局会话sessionId列表，供会话管理, lpushOnRedis
		lpush(UpmsConstant.LAMBKIT_UPMS_SERVER_SESSION_IDS, sessionId.toString());
		// 默认验证帐号密码正确，创建code
		String code = getCode(sessionId, userName);
		// 全局会话的code, setOnRedis
		set(UpmsConstant.LAMBKIT_UPMS_SERVER_SESSION_ID + "_" + sessionId, code, timeOut);
		// code校验值, setOnRedis
		set(UpmsConstant.LAMBKIT_UPMS_SERVER_CODE + "_" + code, code, timeOut);//(int) timeout / 1000
	}

	public String getClientSession(String sessionId) {
		return get(UpmsConstant.LAMBKIT_UPMS_CLIENT_SESSION_ID + "_" + sessionId);
	}

	/**
	 * 更新code有效期
	 * @param sessionId
	 * @param cacheClientSession
	 * @param timeOut
	 */
	public void refreshClientSession(String sessionId, String cacheClientSession, int timeOut) {
		setex(UpmsConstant.LAMBKIT_UPMS_CLIENT_SESSION_ID + "_" + sessionId, cacheClientSession, timeOut);
		expire(UpmsConstant.LAMBKIT_UPMS_CLIENT_SESSION_IDS + "_" + cacheClientSession, timeOut);
	}

	/**
	 * code校验正确，创建局部会话；
	 * 保存code对应的局部会话sessionId，方便退出操作
	 * @param sessionId
	 * @param cacheClientSession
	 * @param timeOut
	 */
	public void saveClientSession(String sessionId, String cacheClientSession, int timeOut) {
		// code校验正确，创建局部会话, getRedisCache().redis()
		setex(UpmsConstant.LAMBKIT_UPMS_CLIENT_SESSION_ID + "_" + sessionId, cacheClientSession, timeOut);
		// 保存code对应的局部会话sessionId，方便退出操作, getRedisCache().redis().
		saddAndExpire(UpmsConstant.LAMBKIT_UPMS_CLIENT_SESSION_IDS + "_" + cacheClientSession, sessionId, timeOut);
	}

	public Long getClientNumber(String code) {
		return scard(UpmsConstant.LAMBKIT_UPMS_CLIENT_SESSION_IDS + "_" + code);
	}

	public String getCode(String code) {
		// 判断是否已登录，如果已登录，则回跳，防止重复登录
		return get(UpmsConstant.LAMBKIT_UPMS_SERVER_CODE + "_" + code);
	}


	///////////////////////////////////

	public void saveSessionOnRedis(String sessionId, String userName, int timeOut) {
		// 全局会话sessionId列表，供会话管理, lpushOnRedis
		getRedisCache().lpush(getUserCacheName(), UpmsConstant.LAMBKIT_UPMS_SERVER_SESSION_IDS, sessionId.toString());
		// 默认验证帐号密码正确，创建code
		String code = getCode(sessionId, userName);
		// 全局会话的code, setOnRedis
		getRedisCache().put(getUserCacheName(), UpmsConstant.LAMBKIT_UPMS_SERVER_SESSION_ID + "_" + sessionId, code, timeOut);
		// code校验值, setOnRedis
		getRedisCache().put(getUserCacheName(), UpmsConstant.LAMBKIT_UPMS_SERVER_CODE + "_" + code, code, timeOut);//(int) timeout / 1000
	}

	/**
	 * 更新code有效期
	 * @param sessionId
	 * @param cacheClientSession
	 * @param timeOut
	 */
	public void refreshClientSessionOnRedis(String sessionId, String cacheClientSession, int timeOut) {
		getRedisCache().setex(getUserCacheName(), UpmsConstant.LAMBKIT_UPMS_CLIENT_SESSION_ID + "_" + sessionId, cacheClientSession, timeOut);
		getRedisCache().expire(getUserCacheName(), UpmsConstant.LAMBKIT_UPMS_CLIENT_SESSION_IDS + "_" + cacheClientSession, timeOut);
	}

	/**
	 * code校验正确，创建局部会话；
	 * 保存code对应的局部会话sessionId，方便退出操作
	 * @param sessionId
	 * @param cacheClientSession
	 * @param timeOut
	 */
	public void saveClientSessionOnRedis(String sessionId, String cacheClientSession, int timeOut) {
		// code校验正确，创建局部会话, getRedisCache().redis()
		getRedisCache().setex(getUserCacheName(), UpmsConstant.LAMBKIT_UPMS_CLIENT_SESSION_ID + "_" + sessionId, cacheClientSession, timeOut);
		// 保存code对应的局部会话sessionId，方便退出操作, getRedisCache().redis().
		getRedisCache().saddAndExpire(getUserCacheName(), UpmsConstant.LAMBKIT_UPMS_CLIENT_SESSION_IDS + "_" + cacheClientSession, sessionId, timeOut);
	}

}
